Pressidian
花园入口
笔记
项目
关于
实验室
GitHub
花园入口
笔记
项目
关于
实验室
GitHub

KNOWLEDGE PATHS

笔记库
当前位置
笔记库/前端/面试/八股/CSS

风格开关实现

1 分钟阅读 · Note

目录树 578 篇

            • 层叠上下文
            • 风格开关实现
            • 响应式布局&移动端优先
            • CSS
            • Flex与Grid布局
            • Position
          • 进阶篇题目列表
          • DOM&浏览器 API
          • HTTP&网络
          • TS
          • Vue
        • 可投递企业
      • 前端技术栈
    • 笔记目录
    • CLAUDE.md
    • Vue 组件与 Render 函数

关联笔记 6

↗层叠上下文同一路径↗响应式布局&移动端优先同一路径↗CSS同一路径↗Flex与Grid布局同一路径↗Position同一路径↗1基础篇共同主题
  • 风格开关实现

风格开关实现

主要是体现语义化


代码实现

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>iOS 开关组件</title>
  <style>
    /* 外层容器 */
    .ios-switch {
      position: relative;
      width: 52px;
      height: 32px;
      display: inline-block;
    }
    /* 原生复选框隐藏 */
    .ios-switch input {
      opacity: 0;
      width: 0;
      height: 0;
    }
    /* 开关轨道(label充当背景轨道) */
    .ios-switch label {
      position: absolute;
      cursor: pointer;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: #e5e5e5;
      border-radius: 32px;
      transition: background 0.3s ease;
    }
    /* 圆形滑块 */
    .ios-switch label::before {
      content: "";
      position: absolute;
      height: 28px;
      width: 28px;
      left: 2px;
      bottom: 2px;
      background-color: white;
      border-radius: 50%;
      box-shadow: 0 1px 3px rgba(0,0,0,0.2);
      transition: transform 0.3s ease;
    }
    /* 选中态:轨道变绿色 */
    .ios-switch input:checked + label {
      background-color: #34c759; /* iOS原生绿色 */
    }
    /* 选中态:滑块右移 */
    .ios-switch input:checked + label::before {
      transform: translateX(20px);
    }
  </style>
</head>
<body>
  <!-- 你面试题目标准结构:div包裹input + label -->
  <div class="ios-switch">
    <input type="checkbox" id="switch">
    <label for="switch"></label>
  </div>
</body>
</html>